﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Miao.ML
{
    public class Serializer
    {
        private static readonly List<Serializer> serializerList = new List<Serializer>();

        public bool serializing => winMLSerializer.serializing;

        private readonly WinMLSerializer winMLSerializer = new WinMLSerializer();

        public static string FastSerialize(object data)
        {
            return FindSerializer().Serialize(data);
        }
        public static void FastSerialize(object data, string filePath)
        {
            FindSerializer().Serialize(data, filePath);
        }
        public static void FastSerialize(object data, string saveDirectory, string fileName, string extension)
        {
            FindSerializer().Serialize(data, saveDirectory, fileName, extension);
        }

        public static Task<string> FastSerializeAsync(object data)
        {
            return FindSerializer().SerializeAsync(data);
        }
        public static Task FastSerializeAsync(object data, string filePath)
        {
            return FindSerializer().SerializeAsync(data, filePath);
        }
        public static Task FastSerializeAsync(object data, string saveDirectory, string fileName, string extension)
        {
            return FindSerializer().SerializeAsync(data, saveDirectory, fileName, extension);
        }

        private static Serializer FindSerializer()
        {
            foreach (var serializer in serializerList)
            {
                if (serializer.serializing) continue;
                return serializer;
            }
            Serializer newSerializer = new Serializer();
            serializerList.Add(newSerializer);
            return newSerializer;
        }

        public Serializer()
        {
            WinMLForUnity.AddToSerializer(winMLSerializer);
        }

        public string Serialize(object data)
        {
            return winMLSerializer.Serialize(data);
        }
        public void Serialize(object data, string filePath)
        {
            winMLSerializer.Serialize(data, filePath);
        }
        public void Serialize(object data, string saveDirectory, string fileName, string extension)
        {
            winMLSerializer.Serialize(data, saveDirectory, fileName, extension);
        }

        public async Task<string> SerializeAsync(object data)
        {
            return await winMLSerializer.SerializeAsync(data);
        }
        public async Task SerializeAsync(object data, string filePath)
        {
            await winMLSerializer.SerializeAsync(data, filePath);
        }
        public async Task SerializeAsync(object data, string saveDirectory, string fileName, string extension)
        {
            await winMLSerializer.SerializeAsync(data, saveDirectory, fileName, extension);
        }

        public void AddCustomSerializationFunction<T>(Func<T, string> serializationFunction)
        {
            winMLSerializer.AddSerializationFunction(serializationFunction);
        }
    }
}